// FacebookApplicationDesigner.cs
// Facebook/FrameworkWeb/Design/Web
// Copyright (c) 2007, Nikhil Kothari. All Rights Reserved.
//

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Web.UI.Design;
using Facebook.Web;

namespace Facebook.Design.Web {

    /// <summary>
    /// The designer associated with the <see cref="FacebookApplication"/> control.
    /// </summary>
    public class FacebookApplicationDesigner : ControlDesigner {

        private DesignerActionListCollection _actionLists;

        /// <summary>
        /// Gets the list of designer actions associated with this designer.
        /// </summary>
        public override DesignerActionListCollection ActionLists {
            get {
                if (_actionLists == null) {
                    _actionLists = base.ActionLists;
                    _actionLists.Add(new FacebookApplicationDesignerActionList((FacebookApplication)Component));
                }
                return _actionLists;
            }
        }

        /// <summary>
        /// Gets the design-time representation of the FacebookApplication control.
        /// </summary>
        /// <returns>The design-time HTML used to implement the designer experience.</returns>
        public override string GetDesignTimeHtml() {
            FacebookApplication fbApplication = (FacebookApplication)Component;

            string instruction;
            if (fbApplication.Mode == FacebookApplicationMode.Fbml) {
                instruction = "FBML Application";
            }
            else {
                instruction = "IFrame Application";
            }

            return CreatePlaceHolderDesignTimeHtml(instruction);
        }

        /// <summary>
        /// Initalizes an instance of a FacebookApplicationDesigner with its associated
        /// component.
        /// </summary>
        /// <param name="component">The associated component.</param>
        public override void Initialize(IComponent component) {
            if (!(component is FacebookApplication)) {
                throw new ArgumentOutOfRangeException("component");
            }

            base.Initialize(component);
        }


        private sealed class FacebookApplicationDesignerActionList : DesignerActionList {

            private FacebookApplication _fbApplication;

            public FacebookApplicationDesignerActionList(FacebookApplication fbApplication)
                : base(fbApplication) {
                _fbApplication = fbApplication;
            }

            public string ApplicationKey {
                get {
                    return _fbApplication.ApplicationKey;
                }
                set {
                    TypeDescriptor.GetProperties(_fbApplication)["ApplicationKey"].SetValue(_fbApplication, value);
                }
            }

            public FacebookApplicationMode Mode {
                get {
                    return _fbApplication.Mode;
                }
                set {
                    TypeDescriptor.GetProperties(_fbApplication)["Mode"].SetValue(_fbApplication, value);
                }
            }

            public string Secret {
                get {
                    return _fbApplication.Secret;
                }
                set {
                    TypeDescriptor.GetProperties(_fbApplication)["Secret"].SetValue(_fbApplication, value);
                }
            }

            public override DesignerActionItemCollection GetSortedActionItems() {
                DesignerActionTextItem labelItem =
                    new DesignerActionTextItem("Application", "App");
                DesignerActionPropertyItem appKeyItem =
                    new DesignerActionPropertyItem("ApplicationKey", "Application Key", "App",
                                                   "The API key used to identify this application.");
                DesignerActionPropertyItem secretItem =
                    new DesignerActionPropertyItem("Secret", "Application Secret", "App",
                                                   "The secret key used to authenticate requests issued by this application.");
                DesignerActionPropertyItem modeItem =
                    new DesignerActionPropertyItem("Mode", "Application Type", "App",
                                                   "The type of user interface implemented by the application.");

                DesignerActionItemCollection items = new DesignerActionItemCollection();
                items.Add(labelItem);
                items.Add(appKeyItem);
                items.Add(secretItem);
                items.Add(modeItem);

                return items;
            }
        }
    }
}
